﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;

//using System.Web.UI.DataVisualization.Charting


namespace DataboundControls
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NORTHWNDEntities db = new NORTHWNDEntities();
            var o = db.Invoices;
                var s = from n in o
                        where n.Salesperson == "Robert King"
                select n;

                foreach (DataboundControls.Invoices c in s)
                {
                    
                    //Page.Controls.Add(new LiteralControl("<p>" + c.Salesperson + "</p>"));
                }

                Chart1.Width = 500;
                Chart1.Height = 500;
                // Display 3D Pie Chart
                Chart1.Series[0].ChartType = SeriesChartType.Pie;
                Chart1.Series[0]["PieLabelStyle"] = "Outside";
                Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;

                // Display a Title
                Chart1.Titles.Add("Salesperson: ");

                // Add Data to Display
                //string[] xValues = { "Rattlesnake Canyon Grocery", "Ernst Handel", "Hungry Owl All-Night Grocers", "Berglunds snabbköp" };

                List<string> xValues = new List<string>(new string[] { "Rattlesnake Canyon Grocery", "Ernst Handel", "Hungry Owl All-Night Grocers", "Berglunds snabbköp", "last item" });
                
                int[] yValues = { 20, 30, 50, 40, 80 };
                Chart1.Series[0].Points.DataBindXY(xValues, yValues);



                
                //Chart1.Series[0].Points[3].Label = "label";
                //Chart1.Series[0].Points[3].AxisLabel = "Hungry Owl All-Night Grocers";


                //Fjerner fra tekst op til højre
                Chart1.Series[0].Points[3].IsVisibleInLegend = true;
            //Viser values i pie-stykkerne
                Chart1.Series[0].Points[3].IsValueShownAsLabel = true;

                


                // Call Out The Letter "D"
                Chart1.Series[0].Points[3]["Exploded"] = "true";

                // Display a Legend
                Chart1.Legends.Add(new Legend("Alphabet"));
                Chart1.Legends["Alphabet"].Title = "Top five companies";
                Chart1.Series[0].Legend = "Alphabet";
        }

        protected void HandleUiListBoxChange(object sender, EventArgs ea)
        {
            //medarbejderens salgs resultater, inklusiv en pie chart af medarbejderens salg fordelt på kunder
            //Page.Controls.Add(new LiteralControl(uiListBox.SelectedValue));



            NORTHWNDEntities db = new NORTHWNDEntities();
            var viewInvoices = db.Invoices;
            var tblEmployees = db.Employees;

            /*var employeeID = from c in viewInvoices
                    join o in tblEmployees on c.Salesperson equals o.FirstName+" "+o.LastName
                    select new { o.EmployeeID };

            foreach (var s in employeeID)
            {
                Page.Controls.Add(new LiteralControl("<h1>" + s.EmployeeID + "</h1>"));
            }*/
            

            int i = int.Parse(uiListBox.SelectedValue);
            Employees emp = (from e in db.Employees
                            where e.EmployeeID == i
                            select e).Single();

            string fullName = emp.FullName;

            var res = (from n in db.Invoices
                       //orderby ( new { f = n.UnitPrice * n.Quantity }).f descending
                       //let topsal = (n.UnitPrice * n.Quantity)
                       //group n by new { sum = n.UnitPrice  * n.Quantity }
                       //orderby topsal descending
                       where n.Salesperson == fullName 
                       select new { compagny = n.CustomerName, total = n.UnitPrice * n.Quantity });//.OrderByDescending(top);//.Take(5);


            var topFive = (from o in res
                       orderby o.total descending
                       select o).Take(5);

            var top = (from o in topFive
                       orderby o.compagny descending
                       select o);
            // Display a Title
            Chart1.Titles[0].Text = "Salesperson: " + fullName;

            //topFive.OrderBy(a => a.total);
            
            
            
            int index = 0;
            int limit = 0;
            int nbr = 0;

            List<string> newXvalues = new List<string>();
            List<int> newYvalues = new List<int>();

            foreach (var s in top)
            {
                newXvalues.Add(s.compagny);
                int total = Convert.ToInt32(s.total);
                newYvalues.Add(total);
                if (total > limit)
                {
                    limit = total;
                    index = nbr;
                }
                nbr++;
            }
            Chart1.Series[0]["PieLabelStyle"] = "Inside";
            Chart1.Series[0].Points.DataBindXY(newXvalues, newYvalues);

            Chart1.Series[0].Points[3]["Exploded"] = "false";
            Chart1.Series[0].Points[index]["Exploded"] = "true";

            //Vis values i pie-stykkerne
            foreach (DataPoint p in Chart1.Series[0].Points)
            {
                p.IsValueShownAsLabel = true;
                nbr++;
            }

        }
        
    }
}